$(function() {
	var teaResume = $("#teaResume");
	teaResume.datagrid({
		url:'resumes/resume',
		fit: true,
		rownumbers: true,
		singleSelect:true,
		pagination: true,
		fitColumns : true,
		columns: [
			[{
				field: 'student.name',
				title: '姓名',
				align: 'center',
				width: 100,
				formatter : function(value, rec) {
					return rec.student['name'];
				}
			}, {
				field: 'student.gender',
				title: '性别',
				align: 'center',
				width: 100,
				formatter: function(value, rec) {
					if(rec.student['gender']=='男'){
						return '<img src="public/images/male.png" title="我是帅锅">'
					}else{
						return '<img src="public/images/female.png" title="我是妹子">'
					}
				}
			}, {
				field: 'title',
				title: '简历标题',
				align: 'center',
				width: 150
			}, {
				field: 'skills',
				title: '专业技能',
				width: 200
			}, {
				field: 'school',
				title: '毕业院校',
				align: 'center',
				width: 150
			}, {
				field: 'ac',
				title: '操作',
				width: 150,
				align: 'center',
				formatter: function(value, row, index) {
					var bts = [];
					bts.push('<a data-id="' + row.id + '" data-index="' + index +
						'" class="ctr more">简历详情</a>');
					if(row.recommend==0){
						bts.push('<a data-id="' + row.id + '" data-index="' + index +
						'" class="ctr recom">推荐</a>');
					}
					if(row.recommend==1){
						bts.push('<a data-id="' + row.id + '" data-index="' + index +
						'" class="ctr outrecom">取消推荐</a>');
					}
					bts.push('<a data-id="' + row.id + '" data-index="' +
						index + '" class="ctr repulse">打回</a>');
					return bts.join('');
				}
			}]
		]
	});
	
	var name = $("#resumename");

	$("#resumeSearch").on('click', function() {
		teaResume.datagrid("load", {
			name : "%" + name.val() + "%"
		});
	});

	var form = $("#resumeSearchForm");
	$("#resumeReset").on('click', function() {
		form.form('clear');
		// 清除查询参数
		teaResume.datagrid("load", {});
	});
	
	var resumePanel = teaResume.datagrid("getPanel");
	resumePanel.on("click", "a.more", function() {
		var index = this.dataset.index;
		// 如果只有下标，没有id的情况，要获取id
		var rows = teaResume.datagrid("getRows");
		// 同下标获取对应行的数据对象
		var id = rows[index].id;
		detailsResume(id);
	}).on("click", "a.repulse", function() {
		var index = this.dataset.index;
		var id = this.dataset.id;

		$.messager.confirm("提示", "您确定要打回此数据吗？", function(r) {
			if (r) {
				$.get("resumes/repulse/" + id, function(rs) {
					if (rs.success) {
						$('#'+id).remove();
						teaResume.datagrid("reload");
					}else{
						var obj = JSON.parse(rs);
						$.messager.alert("提示", obj.message);
					}
				});
			}
		});
	}).on("click", "a.recom", function() {
		var index = this.dataset.index;
		var id = this.dataset.id;
		$.messager.confirm("提示", "您确定要推荐吗？",function(r) {
			if (r) {
				$.get("resumes/getrecom/" + id, function(rs) {
					if (rs.success) {
						teaResume.datagrid("reload");
					}else{
						$.messager.alert("提示", rs.message);
					}
				});
			}
		});
	}).on("click", "a.outrecom", function() {
		var index = this.dataset.index;
		var id = this.dataset.id;
		$.messager.confirm("提示", "您确定要取消推荐吗？",function(r) {
			if (r) {
				$.get("resumes/outrecom/" + id, function(rs) {
					if (rs.success) {
						teaResume.datagrid("reload");
					}else{
						$.messager.alert("提示", rs.message);
					}
				});
			}
		});
	});
	
	function detailsResume(id) {
		// 创建窗口
		var dialog = $("<div/>").dialog({
			href: 'resumes/resume_details' + (id ? ("/" + id) : ""),
			title: "简历详情",
			height: 600,
			width: 650,
			modal: true,
			onClose: function() {
				// 窗口关闭的同时销毁此窗口
				$(this).dialog("destroy");
			},
			buttons: [{
				iconCls: 'icon-save',
				text: '确定',
				handler: function() {
					// 获取到表单对象
					var detailsResume = $("#detailsResume");
					// 校验表单
					if(detailsResume.form('validate')) {
						// serialize方法可以获取到表单所有数据
						$.post("resumes/saveresume", detailsResume.serialize(), function(rs) {
							if(rs.success) {
								// 刷新数据表格
								teaResume.datagrid("reload");
								// 关闭窗口
								dialog.dialog("refresh");
							} else {
								var obj = JSON.parse(rs);
								$.messager.alert("提示", obj.message);
							}
						});
					}
				}
			},{
				iconCls : 'icon-undo',
				text : '取消',
				handler : function(){
					//关闭窗口
					dialog.dialog("close");
				}
			}]
		});
	}
});